var prefix = jsctx + "/evaluate";

var rcEvaluate = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listRcEvaluate",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 10,
                    pageList: [10, 20, 30],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    rightFixedColumns: false,
                    showColumns: false,
                    // cache: false,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            nickName: $("#nickName").val().trim(),
                            orderId: $("#orderId").val().trim(),
                            status: $("#status").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'nickName', title: '用户名', align: 'center', width: '80'},
                        {field: 'orderId', title: '订单编号', width: '80', align: 'center'},
                        {field: 'stationName', title: '站点名称', width: '100', align: 'center'},
                        {field: 'evaluate', title: '评论内容', width: '150', align: 'center'},
                        {field: 'evaluateUrl1', title: '评论图片', width: '200', align: 'center',
                            formatter: function (value, row, index) {
                                if (value != null && value != '') {
                                    return "<img src=" + value + " width='50px' height='50px'>";
                                }
                            }},
                        {field: 'status', title: '状态', width: '200', align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span >未审核</span>'
                                } else if (value == '1') {
                                    return '<span >审核不通过</span>'
                                }else if (value == '2') {
                                    return '<span >审核通过</span>'
                                }
                            }},
                        {
                            title: '操作',
                            field: 'roleId',
                            width: '300', align: 'center',
                            class: 'operate-td',
                            formatter: function (value, row, index) {
                                var action = '';
                                //var b = '';
                                if (row.status=='0'){
                                    action += '<a class="btn btn-success btn-sm " href="#" title="审核" onclick="rcEvaluate.showModel(\''
                                        + row.evaluateId
                                        + '\')">审核</a> ';
                                }
                                action += '<a class="btn btn-success btn-sm " href="#" title="详情" onclick="rcEvaluate.detailEvaluate(\''
                                    + row.evaluateId
                                    + '\')">详情</a> ';
                                action += '<a class="btn btn-success btn-sm " href="#" title="删除" onclick="rcEvaluate.deleteEvaluate(\''
                                    + row.evaluateId
                                    + '\')">删除</a> ';
                                return action;
                            }
                        }
                    ]
                });
    }

    /**
     * 导出列表
     */
    function exportList() {
        $.fileDownload(prefix + "/export", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                nickName: $("#nickName").val().trim(),
                orderId: $("#orderId").val().trim(),
                status: $("#status").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function detailEvaluate(evaluateId) {
        location.href = prefix + "/detailRcEvaluate/"+evaluateId;
    }
    function deleteEvaluate(evaluateId) {
        layer.confirm('确定要删除选中的评价？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteRcEvaluate",
                type: "post",
                data: {
                    'evaluateId': evaluateId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function goBack() {
        location.href = prefix + "/index";
    }


    function showModel(evaluateId) {
        //debugger
        $('#exampleModal').modal('show')

        $("#submitForm").unbind("click").bind("click", function () {
            //debugger
            $.ajax({
                url: prefix + "/updateStatus",
                type: "post",
                data: {
                    "evaluateId": evaluateId,
                    "status": $("#auditStatus").val(),
                    "remark": $("#remark").val()
                },
                dataType: "json",
               /* processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头*/
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                    }

                }
            })
        })
    }




    function elecStationOut(elecStationId) {
        location.href = prefix + "/elecStationOut/" + elecStationId;
    }

    function tenantOperatorStation(elecStationId) {
        location.href = prefix + "/tenantOperatorStation/" + elecStationId;
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        detailEvaluate: detailEvaluate,
        deleteEvaluate: deleteEvaluate,
        exportList: exportList,
        showModel: showModel
    };
})();

var addElecLabel = (function () {
    function init() {
        validateRule();
    };
    var isajaxsend = true;

    function save() {
        var formData = new FormData($("#frmElecLabel")[0]);
        if (isajaxsend) {
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateElecLabel",
                data: formData,
                processData: false,
                contentType: false,
                // async: false,
                error: function (request) {
                    isajaxsend = true
                    console.log(request)
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        isajaxsend = false
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        isajaxsend = true
                        parent.layer.alert(data.message)
                    }

                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmElecLabel").validate({
            rules: {
                labelName: {
                    required: false
                },
                sort: {
                    required: false
                }
            },
            messages: {
                labelName: {
                    required: icon + "请输入标签名称",
                },
                sort: {
                    required: icon + "请输入标签排序",
                },
            },
            submitHandler: function (form) {
                save();
            }
        })
    }
    return {
        init: init
    };
})();

/*

var elecStationOut = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        /!*if (isajaxsend) {
            var formData = new FormData($("#frmElecStationOut")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateElecStationOut",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        javascript:history.back(-1);
                    } else {
                        parent.layer.alert(data.message)
                    }

                },
                complete: function () {
                    isajaxsend = false
                }
            });
        }*!/
    }

    function validateRule() {
       /!* var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmElecStationOut").validate({
            rules: {
                outType: {
                    required: true
                },
                printerType: {
                    required: true
                },
                isFreeParking: {
                    required: true
                },
                isNeedNumber: {
                    required: true
                }
            },
            messages: {
                outType: {
                    required: icon + "请选择出场方式"
                },
                printerType: {
                    required: icon + "请选择打印机类型"
                },
                isFreeParking: {
                    required: icon + "请选择是否免费2个小时停车"
                },
                isNeedNumber: {
                    required: icon + "请选择是否必须车牌号"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })*!/
    }

    return {
        init: init
    };
})();
*/
